<template>
	<view class="header">
		<view class="header__top">
			<view :class="['top__info', stockDetail.zhangdieshu > 0 ?'top__info--up':'']">
				<view class="info__title">{{stockDetail.name}} </view>
				<view class="info__code">{{stockDetail.shuzidaima}}</view>
				<view class="info__price">{{stockDetail.price}}</view>
				<view class="info__des">
					<view class="price__up" v-if="stockDetail.zhangdieshu < 0">
						<uni-icons type="pulldown" color="#2DC065" size="16"></uni-icons>
					</view>
					<uni-icons type="pulldown" color="#CD3C3D" size="16" v-else></uni-icons>
					<text class="des__num">{{stockDetail.zhangdieshu || 0}}</text>
					<text class="des__num">[{{stockDetail.zhangdiebaifenbi || 0}}%]</text>
				</view>
			</view>
			<view class="top__btn top__btn--selected" v-if="isOptional" @click="optionalHandle">
				<text class="btn__title">{{$t('stock.cancel')}}</text>
				<image src="@/static/icons/star_full.png" mode="widthFix" class="btn__icon"></image>
			</view>
			<view class="top__btn" v-else @click="optionalHandle">
				<text class="btn__title">{{$t("stock.add")}}</text>
				<image src="@/static/icons/star.png" mode="widthFix" class="btn__icon"></image>
			</view>
		</view>
		<view class="header__info">
			<view class="info__item">
				<view class="item__group item__today">
					<text class="group__title">{{$t("stock.today")}}</text>
					<text class="group__num">{{stockDetail.open || 0 }}</text>
				</view>
				<view class="item__group item__yester">
					<text class="group__title">{{$t("stock.yesterday")}}</text>
					<text class="group__num">{{stockDetail.close  }}</text>
				</view>
			</view>
			<view class="info__item">
				<view class="item__group item__high">
					<text class="group__title">{{$t("stock.highest")}}</text>
					<text class="group__num">{{stockDetail.high }}</text>
				</view>
				<view class="item__group item__lower">
					<text class="group__title">{{$t("stock.lowest")}}</text>
					<text class="group__num">{{stockDetail.low}}</text>
				</view>
			</view>
			<view class="info__item">
				<view class="item__group item__count">
					<!-- <text class="group__title">{{$t("stock.turnover")}}</text>
					<text class="group__num">{{stockDetail.turnover | formatVol}}</text> -->
				</view>
				<view class="item__group item__limit">
					<text class="group__title">{{$t("stock.value")}}</text>
					<text class="group__num">{{stockDetail.turnover }}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default{
		data(){
			return {
					isOptional:false, //記錄當前股票是否自選
			}
		},
		props:{
			stockDetail:{
				type:Object,
				default:()=>({})
			},
			id:{
				type:String,
				default:""
			}
		},
		filters:{
			formatVol(num){
				let totals = Number(num)
				if(totals>=1000){
						return (totals / 1000).toFixed(2) + "K"
				}
				return totals || 0
			}
		},
		mounted() {
			
			
			// 獲取自選狀態
			this.getOptionalStatus()
		},
		watch: {
		     stockDetail(newval, ordvalue) {
		         console.log(newval,'=======')
		         console.log(ordvalue,'=======1')
		     },
			 deep:true
		 },
		
		methods:{
			// 自選狀態切換
			async optionalHandle(){
				uni.showLoading({
					title: this.$t('loading.loading'),
					mask: false
				});
				let res = await this.$api.addOrDelOptional(this.id)
				uni.hideLoading()
				if(res.code === 1 && res.data){
					uni.showToast({
						title:this.$t('trade.done'),
						icon:"success"
					})
					// 获取自选状态
					this.getOptionalStatus()
				}else{
					uni.showToast({
						title:res.msg,
						icon:"none"
					})
				}
			},
			// 獲取自選狀態
			async getOptionalStatus(){
				let res = await this.$api.getOptionalStatus(this.id)
				if(res.code === 1 && res.data?.msg){
					let {msg} = res.data
					this.isOptional = msg ==="yes" ? true : false
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.header{
		padding: 0 24rpx;
		box-sizing: border-box;
		.header__top{
			@include flexRowAlign(space-between);
			.top__info{
				.info__title{
					font-size: 32rpx;
					color: #fff;
				}
				.info__code{
					color: #fff;
					font-size: 28rpx;
				}
				.info__price{
					font-size: 40rpx;
					font-weight: 800;
					color: #2DC065;
					line-height: 46rpx;
					margin-top: 12rpx;
					@include flexRowAlign();
				
				}
				.info__des{
					font-size: 24rpx;
					font-weight: 500;
					color: #2DC065;
					margin-top: 10rpx;
					@include flexRowAlign();
					.price__up{
						 transform: rotate(180deg);
					}
					.des__num{
						&:last-child{
							margin-left: 10rpx;
						}
					}
				}
			}
			.top__info--up{
				.info__price{
					color: #CD3C3D;
				}
				.info__des{
					color: #CD3C3D;
				}
			}
			.top__btn{
				flex-shrink: 0;
				width: 230rpx;
				height: 62rpx;
				background: #458EF4;
				border-radius: 31rpx;
				@include flexRowAlign(center);
				.btn__title{
					font-size: 20rpx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #FFFFFF;
				}
				.btn__icon{
					width: 24rpx;
					height: 24rpx;
					margin-left: 10rpx;
				}
			}
			.top__btn--selected{
				background: rgba(#fff, 0.1);
			}
		}
		.header__info{
			margin-top: 40rpx;
			@include flexRowAlign(space-between);
			.info__item{
				flex: 1;
				.item__group{
					@include flexRowAlign(space-between);
					font-size: 26rpx;
					color: #999999;
					min-width: 160rpx;
					&:last-child{
						margin-top: 16rpx;
					}
					.group__title{
						flex-shrink: 0;
					}
					.group__num{
						flex: 1;
						margin-left: 12rpx;
						text-align: left;
					}
				}
				.item__today, .item__high{
					.group__num{
						color: #CD3C3D;
					}
				}
				.item__yester, .item__count, .item__limit{
					.group__num{
						color: #fff;
					}
				}
				.item__lower{
					.group__num{
						color: #2DC065;
					}
				}
				.item__count, .item__limit{
					width: 270rpx;
				}
			}
		}
	}
</style>